package com.ndood.oss.aliyun.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ndood.oss.aliyun.bean.ObjectResult;
import com.ndood.oss.aliyun.utils.AliyunOSS;

@Controller
@RequestMapping("/oss/aliyun")
public class FileUploadController {

	@GetMapping("/upload")
	public String toUploadPage(){
		return "oss/aliyun/upload";
	}
	
	/**
	 * 上传单个文件
	 */
	@PostMapping("/upload")
	@ResponseBody
	public Object uploadOneFile(@RequestParam("file") MultipartFile file) throws Exception {

		// Step1: 判断文件内容是否为空
		if (file == null || file.isEmpty()) {
			throw new Exception("文件内容为空!!!");
		}

		// Step2: 上传文件
		String url = AliyunOSS.getInstance().upload("ndood", "test/", file);
		return new ObjectResult("10000", "上传成功！", url);
	}
	
}
